home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / xpipeman / score.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  7.1 KB  |  291 lines

  1. /*
  2.  * score.h  - Xpipeman
  3.  *
  4.  * Send Constructive comments, bug reports, etc. to either
  5.  *
  6.  *          JANET: pavern@uk.ac.man.cs
  7.  *
  8.  *  or      INER : pavern%cs.man.ac.uk@nsfnet-relay.ac.uk
  9.  *
  10.  * All other comments > /dev/null !!
  11.  * 
  12.  * Copyright 1991 Nigel Paver
  13.  * 
  14.  * Permission to use, copy, modify, distribute, and sell this software and its
  15.  * documentation for any purpose is hereby granted without fee, provided that
  16.  * the above copyright notice appear in all copies and that both that
  17.  * copyright notice and this permission notice appear in supporting
  18.  * documentation, and that the author's name not be used in advertising or
  19.  * publicity pertaining to distribution of the software without specific,
  20.  * written prior permission.  The author makes no representations about the
  21.  * suitability of this software for any purpose.  It is provided "as is"
  22.  * without express or implied warranty.
  23.  * 
  24.  * THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING 
  25.  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE 
  26.  * AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY 
  27.  * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN 
  28.  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
  29.  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  30.  * 
  31.  *
  32.  *
  33.  * Acknowledgements to Brian Warkentine (brian@sun.COM) for his xrobots
  34.  * program (copyright  1989) which I cannibalized to write this program.
  35.  * Infact this file is simply lifted staright from xrobots with few
  36.  * if any changes.
  37.  */
  38.  
  39. #include <X11/Intrinsic.h>
  40. #include <X11/StringDefs.h>
  41. #include <X11/Shell.h>
  42.  
  43. #ifdef R3
  44. #include <X11/Box.h>
  45. #include <X11/Command.h>
  46. #include <X11/Label.h>
  47. #else
  48. #include <X11/Xaw/Box.h>
  49. #include <X11/Xaw/Command.h>
  50. #include <X11/Xaw/Label.h>
  51. #endif
  52.  
  53. #include <X11/Xos.h>    /* brings in <sys/file.h> */
  54. #include <stdio.h>
  55. #include "xpipeman.h"
  56.  
  57. /*----------------------------------------------------------------------*/
  58.  
  59. typedef struct {
  60.   char     score[6], 
  61.     name[26];
  62. } SCORE;
  63.  
  64. static SCORE scores[MAXSCORES];
  65.  
  66. void     show_scores(), 
  67.     new_high_score(), 
  68.     load_scores(), 
  69.     write_out_scores();
  70.  
  71. static FILE *scorefile = 0;
  72. char *score_filename;
  73.  
  74. /*----------------------------------------------------------------------*/
  75.  
  76. void
  77. check_score(current_score)
  78.   int current_score;
  79. {
  80.  
  81.   load_scores();
  82.  
  83.   if(current_score > atoi(scores[MAXSCORES-1].score)) {
  84.     new_high_score(current_score);
  85.     write_out_scores();
  86.   }
  87.   if(scorefile) {
  88. #ifndef SYSV
  89.     flock(scorefile->_file, LOCK_UN);
  90. #endif
  91.     fclose(scorefile);
  92.     show_scores();
  93.   }
  94. }
  95.  
  96.  
  97. static void
  98. load_scores()
  99. {
  100.   int i = 0;
  101.  
  102.   if( !(scorefile = fopen(score_filename,"r+")) ) {
  103.     scorefile = fopen(score_filename, "w");
  104.     return;
  105.   }
  106. #ifndef SYSV
  107.   flock(scorefile->_file, LOCK_EX);
  108. #endif
  109.   while( fgets(scores[i].score,6,scorefile)     /* get score */
  110.       && fgets(scores[i].name,26,scorefile)     /* get name */
  111.       && fgetc(scorefile))            /* and newline */
  112.   {
  113.     i++;
  114.     if( i > MAXSCORES ) break;
  115.   }
  116. }
  117.  
  118.  
  119. static void
  120. new_high_score(current_score)
  121.   int current_score;
  122. {
  123.   int i;
  124.   char    *user;
  125.   char textscore[6],
  126.        name[26];
  127.  
  128.   sprintf(textscore,"%5d",current_score);
  129.   user = getenv("USER");
  130.   if (user == (char *)NULL) {
  131.     user = cuserid((char *)NULL);
  132.     if (user == (char *)NULL)
  133.     user = "J. Random Hacker";
  134.   }
  135.   //strncpy(name, user, 25);
  136.   strcpy(name, user);
  137.  
  138.   for(i=MAXSCORES-2;i>=0;i--)
  139.     if( current_score < atoi(scores[i].score) ) {
  140.          /* move new score into i+1 slot */
  141.       strcpy(scores[i+1].score,textscore);
  142.       strcpy(scores[i+1].name,name);
  143.       return;
  144.     } else {
  145.       strcpy(scores[i+1].score,scores[i].score);
  146.       strcpy(scores[i+1].name,scores[i].name);
  147.     }
  148.   /* if it got here, there is a new number 1 score */
  149.   strcpy(scores[0].score,textscore);
  150.   strcpy(scores[0].name,name);
  151.  
  152. }
  153.  
  154.  
  155. static void
  156. write_out_scores()
  157. {
  158.   int i;
  159.  
  160.   if( !scorefile )
  161.     return;
  162.   rewind(scorefile);
  163.   for(i=0;i<MAXSCORES;i++)
  164.     fprintf(scorefile,"%5s%25s\n",scores[i].score,scores[i].name);
  165. }
  166.  
  167.  
  168. /*----------------------------------------------------------------------*/
  169.  
  170.  
  171. Widget score_popup;
  172. Widget score_labels[MAXSCORES];
  173.  
  174. static Arg arglist_score_title[] = {
  175.   {  XtNborderWidth, (XtArgVal) 0  },
  176.   {  XtNresize, (XtArgVal) False  },
  177.   {  XtNwidth, (XtArgVal) 300   },
  178.   {  XtNheight, (XtArgVal) 30   },
  179.   {  XtNlabel, (XtArgVal) "High Scores"  },
  180.   {  XtNjustify, (XtArgVal) XtJustifyCenter  },
  181. };
  182.  
  183. static Arg arglist_score_label[] = {
  184.   {  XtNlabel, (XtArgVal) 0  },
  185.   {  XtNborderWidth, (XtArgVal) 0  },
  186.   {  XtNresize, (XtArgVal) False  },
  187.   {  XtNwidth, (XtArgVal) 300   },
  188.   {  XtNjustify, (XtArgVal) XtJustifyCenter  },
  189. };
  190.  
  191. static Arg arglist_popdown[] = {
  192. /*  {  XtNborderWidth, (XtArgVal) 2  },*/
  193.   {  XtNresize, (XtArgVal) False  },
  194.   {  XtNwidth, (XtArgVal) 300   },
  195.   {  XtNlabel, (XtArgVal) "Pop Down"  },
  196.   {  XtNjustify, (XtArgVal) XtJustifyCenter  },
  197. };
  198.  
  199.  
  200. /*ARGSUSED*/
  201. static XtCallbackProc 
  202. popdown_callback(w, closure, call_data)
  203.   Widget w;
  204.   caddr_t closure;
  205.   caddr_t call_data;
  206. {
  207.   XtPopdown(score_popup);
  208. }
  209.  
  210.  
  211. void
  212. create_high_score_popup(parent)
  213.   Widget parent;
  214. {
  215.   int i;
  216.   Widget score_box, popdown;
  217.  
  218.   score_popup = XtCreatePopupShell(
  219.                                    "score_popup",
  220.                                    transientShellWidgetClass,
  221.                                    parent, 0,0);
  222.  
  223.   score_box = XtCreateManagedWidget(
  224.                                     "score_box",
  225.                                     boxWidgetClass,
  226.                                     score_popup,
  227.                                     0,0);
  228.  
  229.   (void)XtCreateManagedWidget(
  230.                                     "score_title",
  231.                                     labelWidgetClass,
  232.                                     score_box,
  233.                                     arglist_score_title,
  234.                                     XtNumber(arglist_score_title));
  235.  
  236.   for(i=0;i<MAXSCORES;i++) {
  237.     score_labels[i] = XtCreateManagedWidget(
  238.                                     "alabel",
  239.                                     labelWidgetClass,
  240.                                     score_box,
  241.                                     arglist_score_label,
  242.                                     XtNumber(arglist_score_label));
  243.   }
  244.  
  245.   popdown = XtCreateManagedWidget(
  246.                                     "popdown",
  247.                                     commandWidgetClass,
  248.                                     score_box,
  249.                                     arglist_popdown,
  250.                                     XtNumber(arglist_popdown));
  251.   XtAddCallback(popdown,XtNcallback,popdown_callback,0);
  252. }
  253.  
  254.  
  255.  
  256. void
  257. show_scores()
  258. {
  259.   int i;
  260.   char tmp_str[35];
  261.   Arg tmp_arg;
  262.  
  263.   for(i = 0;i<MAXSCORES;i++) {
  264.     sprintf(tmp_str,"%5s  %25s", scores[i].score, scores[i].name);
  265.     XtSetArg(tmp_arg,XtNlabel,tmp_str);
  266.     XtSetValues(score_labels[i],&tmp_arg,1);
  267.   }
  268.   XtPopup(score_popup, XtGrabNone);
  269. }
  270.  
  271.  
  272. /*----------------------------------------------------------------------*/
  273.  
  274.  
  275. /*XtCallbackProc*/
  276. void
  277. show_scores_callback()
  278. {
  279.   load_scores();
  280.  
  281.   if(scorefile) {
  282. #ifndef SYSV
  283.     flock(scorefile->_file, LOCK_UN);
  284. #endif
  285.     fclose(scorefile);
  286.     show_scores();
  287.   }
  288. }
  289.  
  290.  
  291.